import axios from "axios";
import uuidv1 from "uuid/v1";
let mixin = {
  props: {},
  mounted() {},
  data() {
    return {
      gongreds: [],
      gongrecolor: {
        "zhengzhou-model-0-yes.json": "#b399ffb3",
        "zhengzhou-model-0-no.json": "#c0c0c0b3",
        "zhengzhou-model-1-yes.json": "#00ffffb3",
        "zhengzhou-model-1-no.json": "#c0c0c0b3"
      },

      pointericon: {
        隔压站: "geyazhan-01.png",
        燃气锅炉房: "guolufang-01.png",
        热电厂: "redianchang-01.png"
      },

      showdetailpointer: false,
      showteshupointer: false,

      showheight: 9000
    };
  },
  watch: {
    camheight: {
      handler: function(n, o) {
        // this.zoomgongre(n)
      }
    }
  },
  methods: {
    addodlines(data, timeDuration, color) {
      this._odlines = new XE.Obj.ODLines(this._earth);
      this._odlines.translucentPass = true;
      this._odlines.color = color;
      this._odlines.data = data;
      this._odlines.timeDuration = timeDuration;
      this._odlines.playing = true;
    },

    //初始化方法
    gongre() {
      let _this = this;
      var viewer = this._earth.czm.viewer;
      let geos = [
        "zhengzhou-model-0-yes.json",
        "zhengzhou-model-0-no.json",
        "zhengzhou-model-1-yes.json",
        "zhengzhou-model-1-no.json"
      ];

      geos.map(item => {
        var promise = Cesium.GeoJsonDataSource.load("/geojson/" + item);
        let tmp_color = item;
        promise.then(function(dataSource) {
          viewer.dataSources.add(dataSource);
          _this.ds.push(dataSource._name);
          var entities = dataSource.entities.values;

          var timeDuration = 10.0;
          var moveBaseDuration = 0.0;
          var busLines = [];

          for (var o = 0; o < entities.length; o++) {
            var entity = entities[o];

            //删除默认提示框
            entity.billboard = undefined;

            //判断点
            if (entity.id.split(".")[0] == "heatplant") {
              //特殊点
              //热源厂 需要区分隔压站 锅炉房 热源
              switch (entity.properties._type._value) {
                case "隔压站":
                case "燃气锅炉房":
                case "热电厂":
                  if (entity.properties.name._value != "") {
                    if (
                      entity.properties.name._value.indexOf("T") == -1 &&
                      entity.properties.name._value.indexOf("V") == -1
                    ) {
                      // entity.point = new Cesium.PointGraphics({
                      //   color: Cesium.Color.fromCssColorString("#DB181C"),
                      //   pixelSize: 40
                      // });

                      let nameid =
                        entity.id +
                        "|" +
                        entity.properties.pid._value +
                        "|" +
                        entity.position._value;

                      let ln = Cesium.Cartographic.fromCartesian(
                        entity.position._value
                      );

                      let uniqueID = uuidv1();
                      _this._earth.sceneTree.root.children.push({
                        ref: uniqueID,
                        czmObject: {
                          customProp: "ghjghj",
                          xbsjType: "Pin",
                          xbsjGuid: "96f2033d-55cf-4677-b8c7-bde52f14b054",
                          name: nameid,
                          position: [ln.longitude, ln.latitude, ln.height],
                          scale: 0.6,
                          // disableDepthTestDistance: 0,
                          imageUrl:
                            "/configuration/logo/" +
                            _this.pointericon[entity.properties._type._value],
                          pinBuilder: {},
                          far: 1073741824
                        }
                      });

                      _this.pinarr.push(uniqueID);

                    }
                  }
                  break;
                default:
                  break;
              }
            } else if (
              entity.id.split(".")[0] == "substation" ||
              entity.id.split(".")[0] == "tee"
            ) {
              //普通点------------------------------------------------------------------------------
              //热力站
              if (
                entity.id.split(".")[0] == "tee" ||
                entity.id.split(".")[0] == "substation"
              ) {
                //判断视角高度
                //判断是否已经显示
                //普通点
                //------------------------------------++++++++++++++++++++++++++++++
                if (entity.properties.name._value != "") {
                  if (
                    entity.properties.name._value.indexOf("T") == -1 &&
                    entity.properties.name._value.indexOf("V") == -1
                  ) {
                    entity.point = new Cesium.PointGraphics({
                      color: Cesium.Color.fromCssColorString("#5D96B7dd"),
                      pixelSize: 20,
                      distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
                        10.0,
                        52820.0
                      ),
                      scaleByDistance: new Cesium.NearFarScalar(
                        500,
                        2,
                        52820,
                        0.01
                      )
                      // translucencyByDistance: 100,
                      // disableDepthTestDistance: 100.0
                    });

                  }
                }
              }
            } else if (entity.id.split(".")[0] == "pipeline") {

              //-------------------------------------------------------------------------------------------------------------------------------
              //添加 ODline
              var points = [];
              entity.polyline.positions._value.map(item => {
                points.push([
                  Cesium.Cartographic.fromCartesian(item).longitude,
                  Cesium.Cartographic.fromCartesian(item).latitude
                ]);
              });

              let linewidth = 2.0
              if (points.length > 5) {
                // linewidth = 8
              }

              busLines.push({
                positions: points,
                color: [
                  (Math.random() * 0.5 + 0.5) * 1,
                  (Math.random() * 0.8 + 0.2) * 1,
                  0.0,
                  10
                ],
                width: linewidth,
                startTime: timeDuration * Math.random(),
                duration: moveBaseDuration + 1.0 * Math.random()
              });
              entity.polyline = undefined;

              //老版
              // entity.polyline.width = 10; //添加默认样式
              // entity.polyline.material = new Cesium.PolylineGlowMaterialProperty(
              //   {
              //     glowPower: 0.1, //一个数字属性，指定发光强度，占总线宽的百分比。
              //     color: Cesium.Color.fromCssColorString(
              //       _this.gongrecolor[tmp_color]
              //     )
              //   }
              // )
              
            }
          }

          let col = Cesium.Color.fromCssColorString(_this.gongrecolor[tmp_color]);

          _this.addodlines(busLines, timeDuration, [
            col.red,
            col.green,
            col.blue,
            col.alpha * 10
          ]);
        });
      });

      //添加点击事件
      viewer.screenSpaceEventHandler.setInputAction(function onLeftClick(
        movement
      ) {
        var pickedFeature = viewer.scene.pick(movement.position);
        if (typeof pickedFeature != "undefined") {
          if (pickedFeature.id.properties == undefined) {
            //添加点击事件
            _this.pointerclick({
              id: pickedFeature.id.name.split("|")[0],
              pid: pickedFeature.id.name.split("|")[1],
              position: Cesium.Cartographic.fromCartesian(
                pickedFeature.id.position._value
              )
            });
          } else {
            //鼠标是否点到线上
            _this.pointerclick({
              id: pickedFeature.id.id,
              pid: pickedFeature.id.properties.pid._value,
              position: Cesium.Cartographic.fromCartesian(
                pickedFeature.id.position._value
              )
            });
          }
        }
      },
      Cesium.ScreenSpaceEventType.LEFT_CLICK);
    },

    pointerclick(options) {
      //获取接口数据
      //数据返回显示弹窗
      //判断id
      let type =
        options.id.split(".")[0] == "substation" ? "substation" : "heatplant";
      let pid = options.pid;

      let requestBody = {
        // 热源
        queries: [
          {
            metric: "pid,name,m_001t,m_001p,m_002p,m_001q",
            schema: "monitor"
          }
        ],
        table: "HeatPlant",
        search: "pid=" + pid
      };
      if (type == "substation") {
        requestBody = {
          // 热力站
          queries: [
            {
              metric: "pid,name,m_001t,m_001p,m_002p,m_003q,m_003opening",
              schema: "monitor"
            }
          ],
          table: "SubStation",
          search: "pid=" + pid
        };
      }

      if (this.ishistory == "历史") {
        requestBody = {
          queries: [
            {
              metric: "pid,name,m_001t,m_001p,m_002p,m_001q",
              schema: "monitor"
            }
          ],
          table: "HeatPlant",
          search: "pid=" + pid,
          type: "history",
          startTime: this.timerangarr[0].getTime(),
          endTime: this.timerangarr[1].getTime()
        };
        if (type == "substation") {
          requestBody = {
            // 热力站
            queries: [
              {
                metric: "pid,name,m_001t,m_001p,m_002p,m_003q,m_003opening",
                schema: "monitor"
              }
            ],
            table: "SubStation",
            search: "pid=" + pid,
            type: "history",
            startTime: this.timerangarr[0].getTime(),
            endTime: this.timerangarr[1].getTime()
          };
        }
      }

      axios.post("/engipower/open/queryDb.do", requestBody).then(res => {
        //如果有则获取200中的结果
        let data = res.data;
        let html = "";
        if (data.allTotal != 0) {
          if (type == "heatplant") {
            html =
              '<div style="font-size:10px;">' +
              "<p>名称: " +
              data["200"][0].name +
              "</p>\n" +
              "<p>供压: " +
              (data["200"][0].m_001p ? data["200"][0].m_001p : "-") +
              " MPa</p>\n" +
              "<p>回压: " +
              (data["200"][0].m_002p ? data["200"][0].m_002p : "-") +
              " MPa</p>\n" +
              "<p>流量: " +
              (data["200"][0].m_001q ? data["200"][0].m_001q : "-") +
              " t/h</p>\n" +
              "<p>供温: " +
              (data["200"][0].m_001t ? data["200"][0].m_001t : "-") +
              " ℃</p>" +
              "</div>";
          }

          if (type == "substation") {
            html =
              '<div style="font-size:10px;">' +
              "<p>名称: " +
              data["200"][0].name +
              "</p>\n" +
              "<p>一次供压: " +
              (data["200"][0].m_001p ? data["200"][0].m_001p : "-") +
              " MPa</p>\n" +
              "<p>一次回压: " +
              (data["200"][0].m_002p ? data["200"][0].m_002p : "-") +
              " MPa</p>\n" +
              "<p>瞬时流量: " +
              (data["200"][0].m_003q ? data["200"][0].m_003q : "-") +
              " t/h</p>\n" +
              "<p>一次供温: " +
              (data["200"][0].m_001t ? data["200"][0].m_001t : "-") +
              " ℃</p>\n" +
              "<p>阀门开度: " +
              (data["200"][0].m_003opening
                ? data["200"][0].m_003opening
                : "-") +
              " %</p>" +
              "</div>";
          }
        } else {
          html =
            '<div style="font-size:10px;">' +
            // '<p>名称: ' + feature.properties.name + '</p>\n' +
            "<p>未获取到该点数据" +
            "</p>\n" +
            "</div>";
        }

        let uniqueID = uuidv1();
        options.uid = uniqueID;
        this.msgboxarr.push({ uid: uniqueID, diahtml: html, options: options });
      });
    }
  }
};

export default mixin;
